home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / kruskal_wallis.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  214 lines

  1. ; $Id: kruskal_wallis.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ;  Copyright (c) 1991-1997, Research Systems Inc.  All rights
  4. ;  reserved. Unauthorized reproduction prohibited.
  5.  
  6.  
  7.  
  8.          
  9.   
  10. pro kruskal_wallis, Data, Rank, H,Prob, df, Pop,    $
  11.           names=names, List_Name=Ln, Missing=M , NoPrint =NP
  12. ;+ 
  13. ; NAME:
  14. ;    KRUSKAL_WALLIS
  15. ;
  16. ; PURPOSE:
  17. ;    To perform a one-way analysis of variance on k treatments to test
  18. ;    the hypothesis that all treatments have the same distribution 
  19. ;    versus the alternative that at least two treatments differ in 
  20. ;    location.  No assumptions are needed about the underlying probability
  21. ;    distributions.
  22. ;        
  23. ; CATEGORY:
  24. ;    Statistics.
  25. ;
  26. ; CALLING SEQUENCE:
  27. ;    KRUSKAL_WALLIS, Data [,Rank, H, Prob, df, Pop    $
  28. ;         names=names, List_Name=Ln, Missing=M , NoPrint =NP]
  29. ;
  30. ; INPUTS:
  31. ;    Data:    Two-dimensional array. Data(i,j) =the jth   $
  32. ;        observation from the ith treatment;
  33. ;
  34. ; KEYWORDS:  
  35. ;    NAMES:    vector of names for the treatments to be used in the output.
  36. ;
  37. ;    LIST_NAME:    Name of output file.  Default is to the screen.
  38. ;
  39. ;      MISSING:    value used as a place holder for missing data.  Pairwise
  40. ;        handling of missing data.
  41. ;
  42. ;      NOPRINT:    flag, if set, to suppress ouput to the screen or a file.
  43. ;
  44. ; OUTPUT:
  45. ;    Table written to the screen showing rank sum for each treatment.
  46. ;    Also, the kruskal_wallis test statistic and it is probability 
  47. ;    assuming a chi-square distribution are written to the screen.
  48. ;
  49. ; OPTIONAL OUTPUT PARAMETERS:
  50. ;    Rank:    1-dim array of rank sums.
  51. ;        Rank(i) = sum of ranks of treatment i
  52. ;
  53. ;    H:    kruskal_wallis test statistic.
  54. ;
  55. ;    Prob:    probability of H assuming a chi-square distribution.
  56. ;
  57. ;    DF:    degrees of freedom of chi-square distribution.
  58. ;
  59. ;    POP:    1-dim array of treatment sizes
  60. ;
  61. ; RESTRICTIONS:
  62. ;    None.
  63. ;
  64. ; COMMON BLOCKS:
  65. ;    None.
  66. ;
  67. ; PROCEDURE:   
  68. ;    The samples from the k treatments are pooled and their members are
  69. ;    ranked. Let Ri = rank sum for ith treatment and let ni = the sample
  70. ;    size. Let R = the average of all ranks =(n+1)/2 where n = sum(ni).
  71. ;    Let RRi = Ri/ni. The rank sum analogue to the standard sum of squares
  72. ;    is:
  73. ;        SS = sum(ni(RRi -R))^2.
  74. ;    The Kruskal-Wallis statistic H = 12/(n(n+1)) * V and has approximately
  75. ;    the chi-square distribution if each sample size exceeds 4.
  76. ;-
  77.  
  78.  
  79.  
  80.  
  81. On_Error,2
  82. SD = size(Data)
  83.  
  84. if( N_Elements( Ln) NE 0) THEN openw,unit,/get,Ln else unit=-1
  85.  
  86.  
  87. if ( SD(0) NE 2) THEN BEGIN
  88.  printf,unit, 'kruskal-wallis- Data array has wrong dimension'
  89.    goto, DONE
  90. ENDIF
  91.  
  92. C=SD(1)
  93. R= SD(2)
  94.  
  95. Rank = Fltarr(C) ;
  96. Pop  = Lonarr(C);
  97.  
  98. SortI = sort(Data)   
  99. SortD =Data(SortI)              ; sort data
  100.  
  101.  
  102. Miss = N_Elements (M)
  103. if (Miss NE 0) THEN BEGIN            ; discard bad data
  104.  notM = where( SortD NE M, count)
  105.  
  106.  if count NE 0 THEN SortD = SortD(notM) $
  107.  else BEGIN 
  108.      printf,unit,'kruskal_wallis- Too many missing values'
  109.      return
  110.  ENDELSE
  111.  
  112.  SortI = SortI(NotM) 
  113.  
  114. ENDIF
  115.  
  116. size = N_elements(SortI)-1
  117.  
  118. start = 0l
  119. stop  = 0l
  120.  
  121. while start le size DO BEGIN    ;compute rank, average ties
  122.        if start lt size then     $
  123.  
  124.          while (sortd(start) eq sortd(stop)) DO BEGIN
  125.                                       ;how many of same rank
  126.            stop =stop +1     
  127.            if stop gt size THEN goto, fine      
  128.         ENDWHILE $
  129.   
  130.        ELSE  stop = stop+1
  131.        
  132.        fine: 
  133.              IND = SortI(start:stop-1) mod C
  134.              size1 = stop - start - 1
  135.              ranki = start + size1/2.0
  136.              for i = 0l, size1 DO BEGIN
  137.               Rank(IND(i)) = Rank(IND(i))+ ranki +1        
  138.               Pop(IND(i))  = Pop(IND(i)) + 1
  139.               ENDFOR
  140.  
  141.              start = stop
  142.        ENDWHILE    
  143.         
  144.  
  145.  
  146. n = Total(float(Pop))
  147. here = where(Pop ne 0, countC)
  148.  
  149. if countC ne 0 THEN $
  150.  H =12.0/(n*(n+1.)) * Total(Rank(here)^2/Pop(here)) - 3*(n+1) $
  151. else H = 0
  152.  
  153. If CountC ne C THEN BEGIN
  154.      here1 = where (Pop eq 0, count)
  155.      printf, unit, "The following rows are empty and discarded"
  156.      printf, unit, here1
  157. ENDIF
  158.  
  159.  SN =Size(Names)
  160.  if (SN(1) EQ 0) THEN BEGIN
  161.      I = INDGEN(C)
  162.      Names=['pop'+StrTrim(I,2)]  
  163.  ENDIF ELSE                  $
  164.    if ( SN(1) LT C) THEN BEGIN
  165.      I = Indgen(C)
  166.      printf,unit,'sign_test- missing names'
  167.      Names=[Names, 'pop'+StrTrim(I(SN(1):C-1),2)]
  168.   ENDIF
  169.  
  170.  
  171.   DF = CountC-1
  172.  Prob = 1 - chi_sqr1(H,df)
  173.  
  174. if(NOT KEYWORD_SET(NP)) THEN BEGIN  
  175.   printf,unit, " Table of Rank Sums:"
  176.   printf,unit, " Kruskal-Wallis H Test"
  177.   printf,unit, " "
  178.   printf,unit, "   Treatment        count     Rank Sum"
  179.   for i= 0l,C-1 do                       $
  180.      printf,unit, format ='(A13,5x,I8,5X,G15.7)',         $
  181.                                  Names(i),Pop(i),Rank(i)
  182.   printf,unit, " "
  183.   printf,unit,           $
  184.     format = '( " The Kruskal-Wallis H statistic = ",G15.7)',H
  185.  
  186.  printf, unit,format =      $
  187.  '(" probability =", G10.4, " degrees of freedom = ",I5)',  $
  188.             Prob,DF
  189. ENDIF
  190.  
  191. DONE:
  192.    if ( unit NE -1) THEN Free_Lun,unit
  193.  
  194.     RETURN
  195.     END
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.